package com.asion.gankapp.view.fragments;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.asion.gankapp.R;
import com.asion.gankapp.adapters.BaseRecyclerAdapter;
import com.asion.gankapp.applacation.MyApp;
import com.asion.gankapp.utils.MyDerocation;
import com.asion.gankapp.utils.refresh.CircleRefreshLayout;
import com.asion.gankapp.view.activities.WebActivity;

import org.greenrobot.greendao.query.Query;

import java.util.ArrayList;
import java.util.List;

import gl.com.app.Gank;
import gl.com.app.GankDao;

import static android.app.Activity.RESULT_OK;

/**
 * Created by asion on 16-10-6.
 */

public class CollectFragment extends Fragment implements CircleRefreshLayout.OnCircleRefreshListener {

    private RecyclerView recycler;
    private BaseRecyclerAdapter baseRecyclerAdapter;
    private List<Gank> gankList = new ArrayList<>();

    public static CollectFragment newInstance() {
        return new CollectFragment();
    }

    private CircleRefreshLayout refresh;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 100:
                    refresh.finishRefreshing();
                    break;
            }
        }
    };
    private View container;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_base_recycler, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        refresh = ((CircleRefreshLayout) view.findViewById(R.id.refresh_layout));
        container = view.findViewById(R.id.base_recycler_container);
        recycler = ((RecyclerView) view.findViewById(R.id.base_recycler));
        baseRecyclerAdapter = new BaseRecyclerAdapter(getContext());
        baseRecyclerAdapter.setType(1);
        recycler.setItemAnimator(new DefaultItemAnimator());
        recycler.setLayoutManager(new LinearLayoutManager(getContext()));
        recycler.addItemDecoration(new MyDerocation(getContext(), LinearLayout.VERTICAL,1, Color.GRAY));
        recycler.setAdapter(baseRecyclerAdapter);

        addDatas();

        baseRecyclerAdapter.setOnItemClickListener((view1, position, resultsBean, gank) -> {
            Intent intent = new Intent(getActivity(), WebActivity.class);
            Bundle bundle1 = new Bundle();
            bundle1.putSerializable("gank", gank);
            intent.putExtra("bundle", bundle1);
            Bundle bundle = ActivityOptionsCompat.makeCustomAnimation(getContext(), R.anim.in, R.anim.out).toBundle();
            ActivityCompat.startActivityForResult(getActivity(), intent,100, bundle);
        });

        refresh.setOnRefreshListener(this);
    }

    private void addDatas() {
        baseRecyclerAdapter.removeAll();
        Query<Gank> build = MyApp.getGankDao().queryBuilder().where(GankDao.Properties.Cheked.eq(true)).build();
        List<Gank> list = build.list();
        baseRecyclerAdapter.addAll(list);
    }

    @Override
    public void completeRefresh() {
        Snackbar.make(container, "刷新完成", Snackbar.LENGTH_SHORT).show();
    }

    @Override
    public void refreshing() {
        baseRecyclerAdapter.removeAll();
        addDatas();
        handler.sendEmptyMessageDelayed(100, 3000);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 100 && resultCode ==RESULT_OK){
            addDatas();
        }
    }
}
